import {acceptHMRUpdate, defineStore} from "pinia";
import {ref, onMounted, toRaw, Ref,computed} from "vue";

export interface SettingItem {
    api_url: string,
    need_audio: boolean,
    headers: array,
    phone_field: string,
    name_field: string,
    date_field: string,
    remark_field: string,
    address_field: string,
    k: string

}

const store_key: string = "__DEFAULT_SETTING_1";

const __DEFAULT_SETTING: SettingItem = {
    api_url: "https://yanta.crm.xahmyk.cn/api/clue/business_store/",
    k: "Y5o=",
    headers: ['日期', '患者姓名', '所在区域', '咨询详情', '联系方式'],
    phone_field: '联系方式',
    name_field: '患者姓名',
    date_field: '日期',
    remark_field: '咨询详情',
    address_field: '所在区域',
    need_audio: false,
};
export const useSettingStore = defineStore("setting", () => {
    const setting: Ref<SettingItem> = ref({
        api_url: '',
        need_audio: false,
        headers: [],
        k: '',
        phone_field: '',
        name_field: '',
        date_field: '',
        remark_field: '',
        address_field: '',
    });

    function readSetting() {
        console.log("12", "read");
        const item = localStorage.getItem(store_key);
        if (!item)
            setting.value = __DEFAULT_SETTING;
        else {
            const obj = JSON.parse(item);
            setting.value = Object.assign({}, __DEFAULT_SETTING, obj);
        }
        console.log("setting.value", setting.value);
    }

    function saveSetting() {
        localStorage.setItem(store_key, JSON.stringify(toRaw(setting.value)));
    }



    onMounted(() => {
        readSetting();
    })

    return {
        setting,
        saveSetting,
    };
});

if (import.meta.hot)
    import.meta.hot.accept(acceptHMRUpdate(useSettingStore, import.meta.hot));
